package com.zlzxm.shoesmarket.view.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.fragment.FMNavigationClub;
import com.zlzxm.shoesmarket.view.fragment.FMNavigationHome;
import com.zlzxm.shoesmarket.view.fragment.FMNavigationMine;
import com.zlzxm.shoesmarket.view.fragment.FMNavigationRepository;
import com.zlzxm.shoesmarket.view.fragment.FMNavigationStatistic;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/7/21
 */
public class ACHomeNavigation extends BaseActivity implements View.OnClickListener {


    private ViewPager viewPager;

    private LinearLayout llBottom;

    private List<Fragment> pages;

    private String[] bottomItemsText = new String[5];
    private int[] bottomItemsIcon = new int[5];

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_navigation);
        viewPager = ZViewHelp.findById(this,R.id.viewPager);
        llBottom = ZViewHelp.findById(this,R.id.llBottom);

        initBottomView();


        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0; i < llBottom.getChildCount(); i++) {

                    llBottom.getChildAt(i).setSelected(false);
                    if(i == position){
                        llBottom.getChildAt(i).setSelected(true);
                    }

                }
            }
        });

    }


    @Override
    protected void initData() {
        super.initData();
        initPages();
        viewPager.setAdapter(new MyPageAdapter(getSupportFragmentManager()));
        viewPager.setCurrentItem(0);
    }

    //<editor-fold defaultState="collapsed" desc="实现">


    @Override
    public void onClick(View v) {

        int tag = (int) v.getTag();

        viewPager.setCurrentItem(tag);

        for (int i = 0; i < llBottom.getChildCount(); i++) {

            llBottom.getChildAt(i).setSelected(false);

        }

        v.setSelected(true);

    }
    // </editor-fold>


    //<editor-fold defaultState="collapsed" desc="私有方法">


    private void initBottomView() {

        bottomItemsText[0] = getString(R.string.home);
        bottomItemsText[1] = getString(R.string.repository);
        bottomItemsText[2] = getString(R.string.club);
        bottomItemsText[3] = getString(R.string.statistic);
        bottomItemsText[4] = getString(R.string.mine);


        bottomItemsIcon[0] = R.drawable.selector_navigation_home;
        bottomItemsIcon[1] = R.drawable.selector_navigation_repository;
        bottomItemsIcon[2] = R.drawable.selector_navigation_club;
        bottomItemsIcon[3] = R.drawable.selector_navigation_statistic;
        bottomItemsIcon[4] = R.drawable.selector_navigation_mine;

        int itemCount  = llBottom.getChildCount();

        for (int i = 0; i < itemCount; i++) {

            View item  =  llBottom.getChildAt(i);

            if (i == 0 ){

                item.setSelected(true);
            }

            item.setOnClickListener(this);
            item.setTag(i);

            ImageView icon  =  ZViewHelp.findById(item,R.id.llNavigationIcon);
            icon.setImageDrawable(ContextCompat.getDrawable(this,bottomItemsIcon[i]));
            TextView text  =  ZViewHelp.findById(item,R.id.llNavigationText);
            text.setText(bottomItemsText[i]);

        }
    }


    private void initPages(){

        if( pages != null){

            pages.clear();

            pages = null;
        }

        pages = new ArrayList<>();

        pages.add(new FMNavigationHome());
        pages.add(new FMNavigationRepository());
        pages.add(new FMNavigationClub());
        pages.add(new FMNavigationStatistic());
        pages.add(new FMNavigationMine());

    }




    // </editor-fold>


    class MyPageAdapter extends FragmentPagerAdapter {


        public MyPageAdapter(@NonNull FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return pages.get(position);
        }

        @Override
        public int getCount() {
            return pages.size();
        }
    }

}
